---
id: lifecycle
title: Lifecycle
sidebar_label: Lifecycle
---

import useBaseUrl from '@docusaurus/useBaseUrl'

<div class="image-container">
  <img width="283" src={useBaseUrl("img/example.png")} />
</div>

## The `isActive` prop

The Camera's [`isActive`](/docs/api/interfaces/CameraProps#isactive) property can be used to _pause_ the session (`isActive={false}`) while still keeping the session "warm". This is more desirable than completely unmounting the camera, since _resuming_ the session (`isActive={true}`) will be **much faster** than re-mounting the camera view.

For example, you want to **pause** the camera when the user navigates to another page or minimizes the app since otherwise the camera continues to run in the background without the user seeing it. Also, on iOS a green dot indicates the user that the camera is still active, possibly causing the user to raise privacy concerns. (See ["About the orange and green indicators in your iPhone status bar"](https://support.apple.com/en-us/HT211876))

For example, you might want to pause the Camera when the user minimizes the app ([`useAppState()`](https://github.com/react-native-community/hooks?tab=readme-ov-file#useappstate)) or navigates to a new screen ([`useIsFocused()`](https://reactnavigation.org/docs/use-is-focused/)):

```tsx
function App() {
  const isFocused = useIsFocused()
  const appState = useAppState()
  const isActive = isFocused && appState === "active"

  return <Camera {...props} isActive={isActive} />
}
```

## Events

VisionCamera emits multiple lifecycle events inside a Camera session. In general, initialization happens first, and start/stop events are emitted later on in the timeline:

```mermaid
gitGraph LR:
  branch Camera
  checkout Camera
  commit id:" "

  branch Preview

  checkout Time
  commit id:"  "

  checkout Camera
  commit id:"onInitialized()"

  checkout Time
  commit id:"   "
  commit id:"    "

  checkout Camera
  commit id:"onStarted()"

  checkout Preview
  commit id:"onPreviewStarted()"

  checkout Time
  commit id:"     "
  commit id:"      "
  commit id:"       "
  commit id:"        "

  checkout Preview
  commit id:"onPreviewStopped()"

  checkout Camera
  commit id:"onStopped()"
```

### The `onInitialized` event

Once a Camera session has been initialized, the [`onInitialized`](/docs/api/interfaces/CameraProps#oninitialized) event will be fired.
All Camera functions (e.g. [`takePhoto()`](taking-photos)) will be available immediately after the `onInitialized` event has been fired.

The event will be fired everytime Camera outputs are re-created, which happens either if the [`device`](/docs/api/interfaces/CameraProps#device) changes or any output (e.g. [`photo`](/docs/api/interfaces/CameraProps#photo)) is toggled on/off.

### The `onStarted`/`onStopped` events

After the Camera session is initialized it might or might not start streaming - depending on whether [`isActive`](/docs/api/interfaces/CameraProps#isactive) is `true` or `false`.

- `isActive={true}`: The session will start streaming. Once it has started streaming the [`onStarted`](/docs/api/interfaces/CameraProps#onstarted) will be called.
- `isActive={false}`: The session will stop streaming and eventually call the [`onStopped`](/docs/api/interfaces/CameraProps#onstopped) event.

### The `onPreviewStarted`/`onPreviewStopped` events

Similar to the `onStarted`/`onStopped` events, [`onPreviewStarted`](/docs/api/interfaces/CameraProps#onpreviewstarted)/[`onPreviewStopped`](/docs/api/interfaces/CameraProps#onpreviewstopped) will be called when the Camera started or stopped streaming into the Preview View.

This might also happen when flipping Camera devices (e.g. from back -> front), as the Preview View momentarily stops streaming frames when switching devices.

### The `onError` event

If the Camera encounters a runtime error, the [`onError`](/docs/api/interfaces/CameraProps#onerror) event will be fired.
It contains a [`CameraRuntimeError`](/docs/api/classes/CameraRuntimeError) with a `code` and `message` that describe the error.

See ["Camera Errors"](errors) for more information.

## Interruptions

VisionCamera gracefully handles Camera interruptions such as incoming calls, phone overheating, a different app opening the Camera, etc., and will automatically resume the Camera once it becomes available again.

<br />

#### 🚀 Next section: [Camera Formats](formats)
